var entity = require('../entity');
var Topic = entity.Topic;


exports.newAndSave = function(title, content, user, callback){
    var topic = new Topic();
    topic.title = title;
    topic.content = content;
    topic.creator = user.id;
    topic.save(function(err, topic){
        callback(err, topic);
    })
}

exports.findByFaction = function(factionId, callback){
    Topic.find({faction: factionId})
        .sort({createTime:-1})
        .exec(function(err, topics){
        callback(err, topics);
    })
}

exports.findById = function(id, callback){
    Topic.findOne({_id: id}).exec(function(err, topic){
        callback(err, topic);
    })
}

exports.findByCreator = function(userId, callback){
    Topic.find({creator: userId})
        .sort({createTime:-1})
        .exec(function(err, topics){
        callback(err, topics);
    })
}
